//
//  UserPageManager.m
//  JiuKuGospel
//
//  Created by jiukuyinyue on 15-8-10.
//  Copyright (c) 2015年 JiuKuForApp. All rights reserved.
//

#import "UserPageManager.h"
#import "NSString+MD5.h"

#import <CommonCrypto/CommonDigest.h>
#import <CommonCrypto/CommonHMAC.h>

@implementation UserPageManager

IMPLEMENTATION_SINGLETON(UserPageManager)

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.WIFIStr = @"NO";
        self.playLoveStr = @"NO";
        self.musicYinzhi = @"默认";
        self.isWIFIPlay = YES;
        self.qiandaoDic = [NSMutableDictionary dictionary];
    }
    return self;
}

-(void)setUsercode:(NSString *)usercode{
    
    [[NSUserDefaults standardUserDefaults] setObject:usercode forKey:@"usercode"];
}
-(NSString *)usercode{
    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"usercode"] == nil) {
        return @"";
    } else {
        return [[NSUserDefaults standardUserDefaults] objectForKey:@"usercode"];
    }
}
-(void)setUserName:(NSString *)userName{
    
    [[NSUserDefaults standardUserDefaults] setObject:userName forKey:@"userName"];
}
-(NSString *)userName{
    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"userName"] == nil) {
        return @"";
    } else {
        return [[NSUserDefaults standardUserDefaults] objectForKey:@"userName"];
    }
}
-(void)setMobileNum:(NSString *)mobileNum{
    [[NSUserDefaults standardUserDefaults] setObject:mobileNum forKey:@"mobileNum"];
}
-(NSString *)mobileNum{
    return [[NSUserDefaults standardUserDefaults] objectForKey:@"mobileNum"];
}
-(void)setCredit:(NSString *)credit{
    [[NSUserDefaults standardUserDefaults] setObject:credit forKey:@"credit"];
}
-(NSString *)credit{
    return [[NSUserDefaults standardUserDefaults] objectForKey:@"credit"];
}
- (void)saveUserMessages
{
    NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
    
    [userDefaults setObject:self.userID forKey:@"userID"];
    [userDefaults setObject:self.userImageUrl forKey:@"userImageUrl"];
    [userDefaults setObject:self.userName forKey:@"userName"];
    [userDefaults setObject:self.usercode forKey:@"usercode"];
    [userDefaults setObject:self.mbcode_md5 forKey:@"mbcode_md5"];
    [userDefaults setObject:self.groupid forKey:@"groupid"];
    [userDefaults setObject:self.salt forKey:@"salt"];
    [userDefaults setObject:self.play_notenjoy forKey:@"play_notenjoy"];
    [userDefaults setObject:self.play_qlty forKey:@"play_qlty"];
    [userDefaults setObject:self.signin forKey:@"signin"];

    [userDefaults setObject:self.qiandao forKey:@"qiandao"];
    [userDefaults setObject:self.qiandaoDic forKey:@"qiandaoDic"];
    [userDefaults setObject:self.WIFIStr forKey:@"WIFI"];
    [userDefaults setObject:self.playLoveStr forKey:@"playLoveStr"];
    [userDefaults setObject:self.musicYinzhi forKey:@"musicYinzhi"];

    [userDefaults synchronize];

}

- (void)getUserMessages
{
    NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
    
    self.userID = [userDefaults objectForKey:@"userID"];
    self.userImageUrl = [userDefaults objectForKey:@"userImageUrl"];
    self.userName = [userDefaults objectForKey:@"userName"];
    self.mbcode_md5 = [userDefaults objectForKey:@"mbcode_md5"];
    self.groupid = [userDefaults objectForKey:@"groupid"];
    self.salt = [userDefaults objectForKey:@"salt"];
    self.play_notenjoy = [userDefaults objectForKey:@"play_notenjoy"];
    self.play_qlty = [userDefaults objectForKey:@"play_qlty"];
    self.signin = [userDefaults objectForKey:@"signin"];
    self.qiandao = [userDefaults objectForKey:@"qiandao"];
    if ([userDefaults objectForKey:@"qiandaoDic"]) {
        [self.qiandaoDic addEntriesFromDictionary:[userDefaults objectForKey:@"qiandaoDic"]];
    }
    NSString * str = [userDefaults objectForKey:@"WIFI"];
    if (str.length == 0) {
        self.WIFIStr = @"NO";
    }else{
        self.WIFIStr = str;
    }
    self.playLoveStr = [userDefaults objectForKey:@"playLoveStr"];
    self.musicYinzhi = [userDefaults objectForKey:@"musicYinzhi"];
    self.usercode = [userDefaults objectForKey:@"usercode"];


    if ( self.usercode.length !=0) {
        self.isLogin = YES;
    }else{
        self.usercode = @"";
    }
}
//退出的歌曲沙盒存储路径
- (NSString *)filePath:(NSString *)path
{
    NSString * documents = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) firstObject];
    NSString * filePath = [documents stringByAppendingPathComponent:path];
    return filePath;
}
- (void)clearUserMessages
{
    self.userID = nil;
    self.userImageUrl = nil;
    self.userName = nil;
    NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:nil forKey:@"userID"];
    [userDefaults setObject:nil forKey:@"userImageUrl"];
    [userDefaults setObject:nil forKey:@"userName"];
    [userDefaults setObject:nil forKey:@"mbcode_md5"];
    [userDefaults setObject:nil forKey:@"groupid"];
    [userDefaults setObject:nil forKey:@"salt"];
    [userDefaults setObject:nil forKey:@"play_notenjoy"];
    [userDefaults setObject:nil forKey:@"play_qlty"];
    [userDefaults setObject:nil forKey:@"qiandao"];
    [userDefaults setObject:nil forKey:@"usercode"];
    [userDefaults setObject:nil forKey:@"signin"];

    [userDefaults synchronize];
}
#pragma mark - SHA256加密

- (void)hmacSHA256ShouQuanJiamiWithUserCode:(NSString *)userCode
{
    NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
    NSTimeInterval a = [dat timeIntervalSince1970];
    NSString *timeString = [NSString stringWithFormat:@"%.0f", a];    //转为字符型
    
//        NSString *timeString = @"1505200306";
    NSString *appsecret = APPSECRET;
    //算法
    NSString * timekey = [self hmac:timeString withKey:appsecret];
    NSString * userkey = [self hmac:userCode withKey:timekey];
    NSString * accesskey = [self hmac:@"0" withKey:userkey];
    
    self.accesskey = accesskey;
    self.times = timeString;
}
- (NSString *)hmac:(NSString *)data withKey:(NSString *)key {
    const char *keyData = key.UTF8String;
    const char *strData = data.UTF8String;
    uint8_t buffer[CC_SHA256_DIGEST_LENGTH];
    
    CCHmac(kCCHmacAlgSHA256, keyData, strlen(keyData), strData, strlen(strData), buffer);
    return [self stringFromBytes:buffer length:CC_SHA256_DIGEST_LENGTH];
}
#pragma mark - 助手方法
/**
 *  返回二进制 Bytes 流的字符串表示形式
 *
 *  @param bytes  二进制 Bytes 数组
 *  @param length 数组长度
 *
 *  @return 字符串表示形式
 */
- (NSString *)stringFromBytes:(uint8_t *)bytes length:(int)length {
    NSMutableString *strM = [NSMutableString string];
    
    for (int i = 0; i < length; i++) {
        [strM appendFormat:@"%02x", bytes[i]];
    }
    
    return [strM copy];
}
- (NSString*)dictionaryToJson:(NSDictionary *)dic{
    NSError *parseError = nil;
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:0 error:&parseError];
    
    return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}
@end
